package demo.concurrent;

import demo.concurrent.executors.creator.ArrayThreadCreator;
import demo.concurrent.executors.creator.DelayCreator;
import demo.concurrent.executors.myexecutor.Rejected1Handler;
import demo.concurrent.executors.myexecutor.ZThreadPoolExecutor;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 14241
 */
@SpringBootApplication
public class ConcurrentApplication
{

    /**
     * 线程池的5种状态
     * RUN:运行状态，可以接受任务
     * SHUTDOWN:关闭状态，不会在接受任务，但是已经接受的可以继续消费
     * STOP: 不接受新任务，也不执行阻塞队列里面的任务，会中断正在执行的worker
     * TIDYING: workerCount = 0 会去调用terminated()钩子函数
     * TERMINATED: 在terminated()方法执行完之后进入该状态
     * @param args
     * @throws InterruptedException
     */
    public static void main( String[] args ) throws InterruptedException {
        //delayTask();
        //arrayThreadTask();
        //syncThreadTask();
        commitTask();
        Thread.sleep(20000);
    }

    public static void delayTask() throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = DelayCreator.delayThreadPoolExecutor;
        //刚创建好线程池，线程池中没有线程，如果提交来任务，会立即执行任务，需要提前创建好核心线程
        threadPoolExecutor.prestartAllCoreThreads();
        for (int i = 0; i < 100; i++) {
            threadPoolExecutor.execute(new DelayCreator.DelayTask("任务"+i, Long.parseLong(i+"000")));
        }
    }

    public static void arrayThreadTask() throws InterruptedException {
        ThreadPoolExecutor threadPoolExecutor = ArrayThreadCreator.THREAD_POOL_EXECUTOR;
        AtomicInteger atomicInteger = new AtomicInteger(1);
        for (int i = 0; i < 10; i++) {
            threadPoolExecutor.execute(new Thread(()->{
                System.out.println("创建任务："+atomicInteger.incrementAndGet());
            }));
        }
    }

    public static void syncThreadTask() throws InterruptedException {
        SynchronousQueue synchronousQueue = new SynchronousQueue();
        for (int i = 0; i < 10; i++) {
            System.out.println("提交："+i);
            synchronousQueue.put(i);
        }
    }


    public static void commitTask(){
        //阻塞数组队列
        //ZThreadPoolExecutor zThreadPoolExecutor = new ZThreadPoolExecutor(2,3,1L,new ArrayBlockingQueue<>(5),new Rejected1Handler());
        DelayQueue delayTasks = new DelayQueue<>();
        ZThreadPoolExecutor zThreadPoolExecutor = new ZThreadPoolExecutor(2,3,1L, delayTasks,new Rejected1Handler());
        for (int i = 0; i < 100; i++) {
            zThreadPoolExecutor.execute(new DelayCreator.DelayTask("任务"+i, Long.parseLong(i+"000")));
            if(i == 20){
                //关闭线程
                zThreadPoolExecutor.shutdownNow();
            }
        }
    }


    public static final class ZTask implements Runnable {

        private String name;

        public ZTask(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("自定义线程池提交。。。"+name);
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}
